import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemPersonApi } from '#/api/system/person';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDateTime } from '@vben/utils';

import { getRangePickerDefaultProps } from '#/utils';

import DictTag from '../../../components/dict-tag/dict-tag.vue';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'fullName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE, 'string'),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'idcard',
      label: '证件号码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入证件号码',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '姓名',
      field: 'fullName',
    },
    {
      label: '证件类型',
      field: 'cardType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_CARD_TYPE,
          value: val,
        }),
    },
    {
      label: '证件号码',
      field: 'idcard',
    },
    {
      label: '创建时间',
      field: 'createTime',
      render: (val) => formatDateTime(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'fullName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE, 'string'),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'idcard',
      label: '证件号码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入身份证',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<SystemPersonApi.Person>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '人员主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'fullName',
      title: '姓名',
      minWidth: 120,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'idcard',
      title: '证件号码',
      minWidth: 120,
    },

    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
